﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Enter.Entity;
using Enter.Common;
using Enter.BizLogic;
using Enter.Config;
using Enter.Web.SystemAdmin;
namespace WebSystemAdmin.BasicSetting
{
    public partial class ImgDictionaryEdit : PageBase
    {

        public int ID = RequestStrHelper.GetIntFromRequest("ID", 0);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ListCtrlHelper.BindEnumOnListControl(ddl_Status, typeof(EnumStatus));
                ListCtrlHelper.BindEnumOnListControl(ddl_ImgRootType, typeof(ImgRootType));
                ListCtrlHelper.BindEnumOnListControl(ddl_ImgType, typeof(T9003_ImgDictionaryEntity.EnumImgType));
                if (ID != 0)
                {
                    GetInfo();
                }
            }
        }

        private void GetInfo()
        {
            T9003_ImgDictionaryBLLSub bll = new T9003_ImgDictionaryBLLSub();
            T9003_ImgDictionaryEntity model = bll.GetObjectByID(int.Parse(Request.QueryString["ID"]));
            if (model == null) return;
          
            txt_ImgTypeNo.Text = model.ImgTypeNo.ToString();

            this.ddl_ImgRootType.Items.FindByValue(((byte)model.FatherID).ToString()).Selected = true;
            txt_Name.Text = model.Name.ToString();
          
            this.ddl_Status.Items.FindByValue(((byte)model.Status).ToString()).Selected = true;
            txt_ImgWith.Text = model.ImgWith.ToString();
            txt_ImgHigt.Text = model.ImgHigt.ToString();
            txt_FileLength.Text = model.FileLength.ToString();
            this.ddl_ImgType.Items.FindByValue(((byte)model.ImgType).ToString()).Selected = true;
        }

        protected void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                check(txt_ImgTypeNo.Text, txt_ImgWith.Text, txt_ImgHigt.Text,txt_FileLength.Text);
                T9003_ImgDictionaryEntity model = new T9003_ImgDictionaryEntity();
                T9003_ImgDictionaryBLLSub bll = new T9003_ImgDictionaryBLLSub();
                if (Request.QueryString["ID"] != null)
                {
                    model.ID = ID;
                }
                model.ImgTypeNo = txt_ImgTypeNo.Text;
                model.FatherID = (ImgRootType)byte.Parse(ddl_ImgRootType.SelectedValue);
                model.Name = txt_Name.Text;
                model.AddTime = DateTime.Now;
                model.Status = (EnumStatus)byte.Parse(ddl_Status.SelectedValue);
                model.ImgWith = int.Parse(txt_ImgWith.Text);
                model.ImgHigt = int.Parse(txt_ImgHigt.Text);
                model.FileLength = int.Parse(txt_FileLength.Text);
                model.ImgType = (T9003_ImgDictionaryEntity.EnumImgType)byte.Parse(ddl_ImgType.SelectedValue);
                if (bll.Save(model))
                {
                    Utils.throwErrorGotoURL("操作成功", "ImgDictionaryList.aspx");
                }
                else
                {
                    Utils.ShowMessage("操作失败");
                }
            }
            catch (Exception ex)
            {
                Utils.ShowMessage("添加失败:" + ex.Message);
            }
        }

        private void check(string ImgTypeNo,string ImgWith, string ImgHigt, string FileLength)
        {
            if (string.IsNullOrEmpty(ImgTypeNo))
            {
                throw new ArgumentException("编码不能为空");
            }
            if (string.IsNullOrEmpty(ImgWith))
            {
                throw new ArgumentException("宽度不能为空");
            }
            if (!SafeString.IsNumber(ImgWith))
            {
                throw new ArgumentException("宽度输入不正确，请输入数字");
            }
            if (string.IsNullOrEmpty(ImgHigt))
            {
                throw new ArgumentException("高度不能为空");
            }
            if (!SafeString.IsNumber(ImgHigt))
            {
                throw new ArgumentException("高度输入不正确，请输入数字");
            }
            if (string.IsNullOrEmpty(FileLength))
            {
                throw new ArgumentException("大小不能为空");
            }
            if (!SafeString.IsNumber(FileLength))
            {
                throw new ArgumentException("大小输入不正确，请输入数字");
            }
        }

        protected void ddl_ImgRootType_SelectedIndexChanged(object sender, EventArgs e)
        {
            txt_ImgTypeNo.Text = byte.Parse(ddl_ImgRootType.SelectedValue).ToString();
            txt_Name.Text = EnumHelper.GetEnumCNNameFromEnum((Enter.Config.ImgRootType)int.Parse(ddl_ImgRootType.SelectedValue));
        }
        protected void ddl_ImgType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((T9003_ImgDictionaryEntity.EnumImgType)int.Parse(ddl_ImgType.SelectedValue) == T9003_ImgDictionaryEntity.EnumImgType.Square)
            {
                if (string.IsNullOrEmpty(txt_ImgHigt.Text))
                {
                    txt_ImgHigt.Text= txt_ImgWith.Text;
                }
                else {
                    txt_ImgWith.Text = txt_ImgHigt.Text;
                }
                
            }
        }
    }
}
